using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using EstimateTracker.DataAccess.Repositories;
using EstimateTracker.Domain.Entities;
using JetBrains.Annotations;

namespace EstimateTracker.Services
{
    public class ProjectService : IProjectService
    {
        private readonly IProjectRepository _projectRepository;

        public ProjectService([NotNull] IProjectRepository projectRepository)
        {
            if (projectRepository == null) throw new ArgumentNullException("projectRepository");
            _projectRepository = projectRepository;
        }

        public void SaveProject(string projectName, ObservableCollection<Project> projects)
        {
            if (string.IsNullOrEmpty(projectName) || string.IsNullOrWhiteSpace(projectName)) return;
            Project project = new Project { Name = projectName };
            projects.Add(project);
            _projectRepository.Save(project);
        }

        public void FindAllProjects(ObservableCollection<Project> observableCollection)
        {
            List<Project> projects = _projectRepository.FindAll().ToList();
            observableCollection.Clear();
            if (projects != null)
            {
                projects.ForEach(observableCollection.Add);
            }
        }
    }
}